package demo2.observerImpl;

import demo2.DisplayElement;
import demo2.Subject;
import demo2.subjectImpl.WeatherData;

import java.util.Observable;
import java.util.Observer;

/**
 * @author 24岁没牵过女人的手 日期：2023/6/29 17:17 文件描述：
 */
public class HeatIndexDisplay implements Observer, DisplayElement {
    private double temp;
    private double humidity;
    private double pressure;

    private Observable observable;

    public HeatIndexDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {
        System.out.println("HeatIndex is : "+computeHeatIndex(temp, humidity));
    }


    private float computeHeatIndex(double t, double rh) {
        return (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) +
                (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) +
                (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
                (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *
                (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
                (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
                0.000000000843296 * (t * t * rh * rh * rh)) -
                (0.0000000000481975 * (t * t * t * rh * rh * rh)));
    }

    @Override
    public void update(Observable o, Object arg) {
        WeatherData observable = (WeatherData) o;
        this.temp = observable.getTemperature();
        this.humidity = observable.getHumidity();
        display();
    }
}
